<?php

/* @var $this yii\web\View */

use yii\helpers\Html;

?>
<div class="container container-box fill-height">
    <?php
    if(Yii::$app->controller !== null) {
        echo Yii::$app->controller->renderPartial('ucenter_side_menu');
    }
    ?>
    <div class="side-main pull-left">
        <span class="txt-first"><?= '创建分组' ?></span><br/><br/>
        <form id="creategroup" class="create-group" method="post" action="/ucenter/ajaxcreategroup">
            <input type="text" name="group_name" placeholder="分组名称" class="editor" autocomplete="off"/><br/><br/>
            <input type="text" name="keywords" placeholder="关键词" class="editor" autocomplete="off"/><br/><br/>
            <input type="text" name="intro" placeholder="简介" class="editor" autocomplete="off"/><br/><br/>
            <p>选择分类</p>
            <?php
            if($category_list !== null
                    && count($category_list) > 0) {
                foreach($category_list as $list_item) {
                    $txt_html = '<span class="span-nowrap"><input type="checkbox" name="category_ids[]" value="%s"/>%s</span>';
                    $txt_html = sprintf($txt_html, $list_item['id'], Html::encode($list_item['name']));
                    echo $txt_html;
                }
            }
            ?>
            <br/><br/>
            <input type="submit" value="创建分组" class="btn"/><br/>
        </form>
        <p id="p-t-1"></p>
        <p id="p-t-2"></p>
        <p id="p-t-3"></p>
    </div>
</div>
<!-- jqeury.form.js -->
<?php $this->beginBlock('work') ?>
    $().ready(function() {
        <?php Yii::info('bind submit'); ?>
        $(document).ready(function() {
            var opts = {
                beforeSubmit: function(formData, $jqForm, options) {
                    $('#p-t-1').html('beforeSubmit');
                    var inputSubmit = $jqForm.find("input[type='submit']");
                    if(inputSubmit.hasClass("btn-disable")) {
                        $('#p-t-3').html('btn disable');
                        return false;
                    }
                    $jqForm.find("input[type='submit']").addClass("btn-disable").attr("value", "创建中");
                    return true;
                },
                success: function(respTxt, statusTxt, xhr, $jqForm) {
                    $jqForm.find("input[type='submit']").removeClass("btn-disable").attr("value", "创建分组");
                    $jqForm.resetForm();

                    $('#p-t-2').html('resp: ' + respTxt);
                    var resp_obj = JSON.parse(respTxt);
                    if(0 === resp_obj.code) {
                        $('#p-t-2').html('succed: ' + respTxt);
                    } else {
                        $('#p-t-2').html('failed: ' + respTxt);
                    }
                }
            };
            $('#creategroup').ajaxForm(opts);
        });
    });
<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['work'], \yii\web\View::POS_END); ?>